import datetime
import random

from config import app
from info import db
from info.models import User


def add_test_user():
    """添加１００００个用户"""
    # 获取当前时间
    now=datetime.datetime.now()
    users=[]
    try:

        for i in range(10000):

            user=User()
            user.mobile = "%011d" % i
            user.nick_name ="%011d" % i
            user.password_hash='pbkdf2:sha256:50000$SgZPAbEj$a253b9220b7a916e03bf27119d401c48ff4a1c81d7e00644e0aaf6f3a8c55829'
            user.last_login=now - datetime.timedelta(seconds=random.randint(0,2678400))

            users.append(user)
            print(user.mobile)
    except Exception as e:
        print(e)
    from manager import app
    with app.app_context():
        db.session.add_all(users)
        db.session.commit()

    print('ok')

if __name__ == '__main__':
    add_test_user()